﻿using System;
using System.Threading;
using IOSendInterface;
using PipeServer.Enum;

namespace PipeServer.Model
{
    internal class Page : ICloneable
    {
        private static readonly PageType _SingleDataPage = PageType.数据包 | PageType.单向;
        private static int _PageId = 1;

        private static int _GetPageId()
        {
            return Interlocked.Increment(ref _PageId);
        }

        public static Page GetSysPage(string type, string str)
        {
            return new Page
            {
                PageType = PageType.系统包,
                SendData = str,
                SendType = SendType.字符串,
                Type = type
            };
        }


        public static Page GetDataPage(string pipeName, Guid clientId, string type, int timeOut, int syncTimeOut)
        {
            return new Page
            {
                PipeName = pipeName,
                ClientId = clientId,
                PageType = PageType.数据包,
                PageId = _GetPageId(),
                SendData = Array.Empty<byte>(),
                SendType = SendType.字节流,
                Type = type,
                TimeOut = timeOut,
                SyncTimeOut = syncTimeOut
            };
        }
        public static Page GetDataPage<T>(string pipeName, Guid clientId, string type, T data, int timeOut, int syncTimeOut)
        {
            return new Page
            {
                PipeName = pipeName,
                ClientId = clientId,
                PageType = PageType.数据包,
                PageId = _GetPageId(),
                SendData = data,
                SendType = SendType.对象,
                Type = type,
                TimeOut = timeOut,
                SyncTimeOut = syncTimeOut
            };
        }

        public static Page GetSingleDataPage(string pipeName, Guid clientId, string type, int timeOut, int syncTimeOut)
        {
            return new Page
            {
                PipeName = pipeName,
                ClientId = clientId,
                PageType = _SingleDataPage,
                PageId = -1,
                SendData = Array.Empty<byte>(),
                SendType = SendType.字节流,
                Type = type,
                TimeOut = timeOut,
                SyncTimeOut = syncTimeOut
            };
        }
        public static Page GetSingleDataPage<T>(string pipeName, Guid clientId, string type, T data, int timeOut, int syncTimeOut)
        {
            return new Page
            {
                ClientId = clientId,
                PipeName = pipeName,
                PageType = _SingleDataPage,
                PageId = -1,
                SendData = data,
                SendType = SendType.对象,
                Type = type,
                TimeOut = timeOut,
                SyncTimeOut = syncTimeOut
            };
        }
        public static Page GetBatchDataPage<T>(string type, T data, int timeOut, int syncTimeOut)
        {
            return new Page
            {
                PageType = _SingleDataPage,
                SendData = data,
                PageId = -1,
                SendType = SendType.对象,
                Type = type,
                TimeOut = timeOut,
                SyncTimeOut = syncTimeOut
            };
        }
        public static Page GetReply(GetDataPage arg, object data)
        {
            return new Page
            {
                SendData = data,
                SendType = ToolsHelper.GetSendType(data.GetType()),
                Type = arg.Type,
                PageType = PageType.回复包,
                PageId = arg.PageId,
            };
        }
        public static Page GetReplyPage(GetDataPage arg, object data)
        {
            return new Page
            {
                SendData = data,
                SendType = ToolsHelper.GetSendType(data.GetType()),
                Type = arg.Type,
                PageType = arg.PageType == PageType.系统包 ? arg.PageType : PageType.回复包,
                PageId = arg.PageId,
            };
        }

        public Guid ClientId;
        public string PipeName;
        /// <summary>
        /// 父ID
        /// </summary>
        internal int PageId;
        /// <summary>
        /// 需要发送的数据
        /// </summary>
        internal object SendData;
        /// <summary>
        /// 类型
        /// </summary>
        internal string Type;

        /// <summary>
        /// 数据包的类型
        /// </summary>
        internal Enum.PageType PageType;

        /// <summary>
        /// 发送的类型
        /// </summary>
        internal SendType SendType;
        public int TimeOut;
        public int SyncTimeOut;

        public object Clone()
        {
            return this.MemberwiseClone();
        }
    }
}
